/**
 * 
 */
package org.sunnyleaf.votedemo.client.ui;

import org.sunnyleaf.votedemo.client.GreetingServiceAsyncFactory;
import org.sunnyleaf.votedemo.client.Votedemo;
import org.sunnyleaf.votedemo.client.Votedemo.UPDATE_TYPE;
import org.sunnyleaf.votedemo.client.data.Vote;
import org.sunnyleaf.votedemo.client.data.VoteRecord;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author Administrator
 * 
 */
public class VoteListItemOwner extends Composite {

	private Vote vote;
	private VerticalPanel mainpanel = new VerticalPanel();
	private VerticalPanel resPanel = new VerticalPanel();
	private FlexTable ft;
	private Votedemo votedemo;
	
	public void setMainPanel( Votedemo demo ){
		this.votedemo = demo;
	}
	
	public VoteListItemOwner(Vote v) {
		this.vote = v;
		this.ft = new FlexTable();
		this.mainpanel.setWidth("100%");
		this.resPanel.setWidth("100%");
		this.mainpanel.add(ft);
		update();
		this.mainpanel.add(resPanel);
		initWidget(mainpanel);		
	}
	
	public Vote getVote(){
		return vote;
	}
	
	@SuppressWarnings("deprecation")
	void update(){
		final FlexCellFormatter cf = ft.getFlexCellFormatter();
		
		ft.setWidth("100%");
		ft.setCellSpacing(3);
		ft.setCellPadding(3);

		ft.setWidget(0, 0, new HTML("投票名称："));
		ft.setWidget(0, 1, new HTML(vote.getVoteName()));
		cf.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		ft.setWidget(0, 2, new HTML("发起时间："));
		ft.setWidget(0, 3, new HTML(DateTimeFormat.getMediumDateTimeFormat().format(vote.getStartDate())));
		cf.setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_LEFT);

		ft.setWidget(1, 0, new HTML("参加人数："));
		int percent = vote.getTotalCount()==0?0:(vote.getVotedCount() * 100 / (vote.getTotalCount()));
		ft.setWidget(1, 1, new HTML(vote.getVotedCount() + "/" + vote.getTotalCount() + "(" + percent + "%)"));
		cf.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
		ft.setWidget(1, 2, new HTML("状态："));
		ft.setWidget(1, 3, new HTML(vote.getStatus()));
		cf.setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_LEFT);
		


		HorizontalPanel hp = new HorizontalPanel();
		hp.setWidth("80%");
		Hyperlink viewList = new Hyperlink("查看邀请列表","#");
		viewList.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				final Button closeButton = new Button("Close");
				final DialogBox dialogBox = new DialogBox(true);
				closeButton.getElement().setId("closeButton");
				dialogBox.setText("邀请列表");
				dialogBox.setSize("400px", "400px");
				dialogBox.center();
				dialogBox.setAnimationEnabled(true);
				final VerticalPanel dialogVPanel = new VerticalPanel();
				dialogVPanel.addStyleName("dialogVPanel");
				dialogBox.setWidget( dialogVPanel );
				dialogBox.show();
				
				closeButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogBox.hide();
					}
				});
				
				final ListBox listbox = new ListBox(true);
				listbox.setWidth("350px");
				listbox.setHeight("350px");
				
				GreetingServiceAsyncFactory.getServiceInstance().getVoteRecords(vote.getId(), new AsyncCallback<List<VoteRecord>>() {
					public void onFailure(Throwable caught) {
						Window.alert("Get Vote Record Error");
					}

					public void onSuccess(List<VoteRecord> result) { 
						for( VoteRecord record : result ){
							String itemstr = record.getAccount();
							itemstr += record.isVoted()?"   (voted)":"   (not voted)";
							listbox.addItem( itemstr );
						}
					}
				});
				
				dialogVPanel.clear();
				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
				dialogVPanel.add( listbox );
				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
				dialogVPanel.add(closeButton);
				dialogBox.center();
			}
		});
		hp.add(viewList);
		
		if (vote.getStatus().equals(Vote.OPEN)) {
			Hyperlink reSend = new Hyperlink("发送提醒邮件","#");
			reSend.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					// TODO 发送提醒邮件
					
				}
			});
			Hyperlink endVote = new Hyperlink("结束投票","#");
			endVote.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					vote.setStatus(Vote.CLOSED);
					
					if( vote.getOwner().equals( Votedemo.getEmail())){
						votedemo.updateAttendVotePanel(vote, Votedemo.UPDATE_TYPE.UPDATE, false );
					}
					
					GreetingServiceAsyncFactory.getServiceInstance().saveVote(vote, 
						new AsyncCallback<String>(){

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("远程调用失败");
							}

							@Override
							public void onSuccess(String result) {
								update();					
							}
							
						});
				}
			});
			hp.add(reSend);
			hp.add(endVote);
		}
		else {
			Hyperlink checkRes = new Hyperlink("查看结果","#");
			checkRes.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					// 查看投票结果，最好用柱状图显示
					if( resPanel.getWidgetCount() != 0 ){
						resPanel.clear();
					}
					else {
						resPanel.add( new VoteResultPage( vote ));
					}
				}
			});
			
			Hyperlink sendRes = new Hyperlink("发送结果","#");
			sendRes.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					// TODO 发送邮件..
				}
			});
			
			Hyperlink delVote = new Hyperlink("删除投票","#");
			delVote.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					if( vote.getOwner().equals( Votedemo.getEmail() ) ){
						Votedemo demo = votedemo;
						demo.updateAttendVotePanel(vote, Votedemo.UPDATE_TYPE.DELETE, false );
					}
					GreetingServiceAsyncFactory.getServiceInstance().deleteVoteByID( vote.getId(), new AsyncCallback<String>() {
						
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("远程调用失败");
						}

						@Override
						public void onSuccess(String result) {
							deleteSelf();
						}
					
					});
				}
			});
			hp.add(checkRes);
			hp.add(sendRes);
			hp.add(delVote);
		}
		ft.setWidget(2, 0, hp);
		cf.setColSpan(2, 0, 4);

		ft.setStyleName("vl-flextable");
	}
	void deleteSelf(){
		Panel parent = (Panel)this.getParent();
		parent.remove(this);
	}
}
